
define(['angular',"text!www/user/user.html",'url','common','jqload'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($scope,$rootScope,$location,$http){
	   hideMenu();
	   initFooter(4);
	   	showSpin();
	   	
	   	
	   	$scope.comment_tab = 0;
	   	$scope.suipai_tab = 1;
	   	
	   	$scope.comment = function()
	   	{
//	   		$scope.comment_active = "active";
//	   		$scope.suipai_active = "";
		   	$scope.activeline = "moveactiveleft";
		   	$scope.suipai_tab = 0;
		   	$scope.comment_tab = 1;
//		   	alert(1);
		   	this.busy = false;
		   	if($rootScope.notify > 0)
		   	{
		   		if (this.busy) return;
		         this.busy = true;
		         
		         var url = cl.commentNotify+"&callback=JSON_CALLBACK" ;
		         $http.jsonp(url).success(function (res) {
					
		        	 
		        	 
		        	 this.busy = false;
		         });
		         
		         
		         //$scope.notify_comment = 0;
					
				$rootScope.notify = 0;
				
				//cl.user.notify_comment = 0;

				cl.user.notify = 0;
					
		   	}
		   	
		    
	         
		   	
			
			
			
	   	}
	   	
	   	$scope.suipai = function()
	   	{
	   		$scope.activeline = "moveactiveright";
//		   	$scope.comment_active = "";
//		   	$scope.suipai_active = "active";
		   	$scope.comment_tab = 0;
		   	$scope.suipai_tab = 1;
	   	}
	   	
	     $scope.user = {};
    	 $scope.user.busy = false;
    	 $scope.user.isLogin = false;
    	 $scope.user.isMember = false;
    	 $scope.user.isShow = false;
    	 $scope.user.suipai = [];
    	 $scope.user.comment = [];
    	 $scope.user.ip = ip;
		 $scope.user.isLoginFunc = function () {
	    	 if (this.isLogin){
	    		 hideSpin();
	    		 return;
	    	 }
	         if (this.busy) return;
	         this.busy = true;
	         var pro = this;
	         var url = cl.isLoginUrl+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02001"){//已登录
						pro.isLogin = true;
						pro.initUser();
						
						
						
						
						
					}else if(res.code=="CL02002"){//未登陆
						pro.isLogin = false;
						hideSpin();
						pro.isShow = true;
						setTimeout("initUserScroll();",1000);
					}
					pro.busy = false;
	         });
	     };
	     $scope.user.initUser = function(){
	    	 
	    	 var pro = this;
	    	 var url = cl.getLoginInfoUrl2+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02003"){//登录失效
						pro.isLogin = false;
						hideSpin();
					}else if(res.code=="CL02004"){//获取信息
						//console.info(this.items);
						cl.user.initUser(res.result.user);
						
						$rootScope.notify = res.result.user.notify;
						//console.log(cl.user);
						
//						$rootScope.notify＝res.result.user.notify;
						
						//$scope.notify_comment = $rootScope.notify ＝ cl.user.notify;
						
						//showAlert($scope.notify_comment);
//						alert($rootScope.notify);
						//$scope.notify = 0;
						//$rootScope.notify = 0;
						
						pro.suipai = res.result.suipai;
						pro.comment = res.result.comment;
						pro.isMemberFunc();
						hideSpin();
						//console.info(this.items);
					}else if(res.code=="CL02005"){//获取信息失败
						hideSpin();
					}
					
					
//					
//					console.log(222);
					setTimeout("lazyloadImage();initUserScroll();",1000);
//					$timeout(function(){
					pro.isShow = true;
					
					
					
				
	         });
	     };
	     $scope.user.toLogonFunc = function (successCallback,failCallback) {
	         if (this.busy){
	        	 hideSpin();
	        	 return;
	         }
	         this.busy = true;
	         var url = cl.isLoginUrl+"&callback=JSON_CALLBACK" ;
	         var pro = this;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02001"){//已登录
						successCallback(res.result.openid);
					}else if(res.code=="CL02002"){//未登陆
						pro.isLogin = false;
						failCallback();
					}
					pro.busy = false;
	         });
	     };
	     $scope.user.isMemberFunc =  function(){
	 		if(cl.user.mobile_phone!=""&&cl.user.mobile_phone!=undefined&&cl.user.mobile_phone!=null){
	 			this.isMember = true;
	 			return false;
	 		}
	 		this.isMember = false;
	 	};
	 	$scope.user.delComment = function(id,index,news_id){
			if (this.busy)
			{
		       	 return;
	        }
		    this.busy = true;
		    showSpin();
			var url = cl.delCommentUrl+"&callback=JSON_CALLBACK&id="+id+"&wxid="+cl.user.id+"&news_id="+news_id;
			var pro = this;
			showConfirm("确定删除？",function()
			{
				 $http.jsonp(url).success(function (res) {
						if(res.code=="CL02043"){//删除成功
							showAlert("成功删除评论");
//							pro.news_info.comment_num = parseInt(pro.news_info.comment_num)-1;
			    			pro.comment.splice(index,1);
						}else{
							showAlert("删除评论失败");
						}
						pro.busy = false;
						hideSpin();
				 })
			},function(){
				pro.busy = false;
				hideSpin();
			});
		}
	 	
	 	
		$scope.item = cl.user;
		$scope.user.isLoginFunc();
		$scope.goMyInfo = function (path, pageAnimationClass){
			showSpin();
			function success(openid){
				window.location.href = cl.goUserInfoUrl+"&openid="+openid;
			}
			function fail(){
				window.location.href=cl.wxLoginUrl;
			}
			$scope.user.toLogonFunc(success,fail);
		};
		$scope.user.delNews = function(id,index){
	 		if (this.busy)
			{
		       	 return;
	        }
		    this.busy = true;
		    showSpin();
			var url = cl.delNewsUrl+"&callback=JSON_CALLBACK&id="+id+"&wxid="+cl.user.id;
			var pro = this;
			showConfirm("确定删除？",function()
			{
				 $http.jsonp(url).success(function (res) {
						if(res.code=="CL02048"){//删除成功
							showAlert("成功删除该帖子");
			    			pro.suipai.splice(index,1);
						}else{
							showAlert("删除帖子失败");
						}
						pro.busy = false;
						hideSpin();
				 })
			},function(){
				pro.busy = false;
				hideSpin();
			});
	 	};
	 	$scope.user.nextPage = function(){
	 	}
    };
    return {controller:controller,tpl:tpl};
});
function initUserScroll(){
	//会员中心滚动导航固定
	$(window).on('scroll',function(){
		var scrollPos = $(window).scrollTop(),winH = $('.banner-img').height() + $('.user-btn').height();
		if(scrollPos > winH){
			$('.user-tab-con').addClass('user-tab-fixed').next().addClass('user-tab-fixed-block').end().find('a').attr('href','#tab-content');
		}else {
			$('.user-tab-con').removeClass('user-tab-fixed').next().removeClass('user-tab-fixed-block').end().find('a').removeAttr('href','#tab-content');
		}
	})
}